package com.itheima.web.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.Freeze;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.service.db.FreezeService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.service.mongo.UserLocationService;
import com.itheima.service.mongo.VideoService;
import com.itheima.vo.CommentVo;
import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.*;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class UserManager {

    @DubboReference
    private UserInfoService userInfoService;
    @DubboReference
    private VideoService videoService;

    // 分页查询
    public ResponseEntity findUserInfoByPage(Integer pageNum, Integer pageSize) {
        // 1.调用rpc查询
        PageBeanVo pageBeanVo = userInfoService.findUserInfoByPage(pageNum, pageSize);

        // 2.返回
        return ResponseEntity.ok(pageBeanVo);
    }

    // 用户详情
    public ResponseEntity findUserInfoById(Long userId) {
        // 1.调用rpc查询
        UserInfo userInfo = userInfoService.findUserInfoById(userId);
        // 2.返回
        return ResponseEntity.ok(userInfo);
    }

    @DubboReference
    private MovementService movementService;

    // 动态列表
    public ResponseEntity findMovementVoByPage(Integer state, Long userId, Integer pageNum, Integer pageSize) {
        // 1.调用rpc查询
        PageBeanVo pageBeanVo = movementService.findMovvementByPage(state, userId, pageNum, pageSize);
        // 2.封装vo
        // 2.1 声明voList
        List<MovementVo> voList = new ArrayList<>();
        // 2.2 获取movementList
        List<Movement> movementList = (List<Movement>) pageBeanVo.getItems();
        // 2.3 遍历movementList
        if (CollectionUtil.isNotEmpty(movementList)) {
            for (Movement movement : movementList) {
                // 创建vo
                MovementVo vo = new MovementVo();
                vo.setUserInfo(userInfoService.findUserInfoById(movement.getUserId()));
                vo.setMovement(movement);
                // 指定动态时间
                vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));

                // 添加到集合
                voList.add(vo);
            }
        }
        // 3.将vo设置到分页对象
        pageBeanVo.setItems(voList);

        // 4.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

    // 动态详情
    public ResponseEntity findMovementVoById(String publishId) {
        // 1.调用rpc查询
        Movement movement = movementService.findById(new ObjectId(publishId));

        // 2.封装vo
        // 创建vo
        MovementVo vo = new MovementVo();
        vo.setUserInfo(userInfoService.findUserInfoById(movement.getUserId()));
        vo.setMovement(movement);
        // 指定动态时间
        vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));

        // 3.返回vo
        return ResponseEntity.ok(vo);
    }

    @DubboReference
    private CommentService commentService;

    // 动态评论列表
    public ResponseEntity findCommentVoByPage(String publishId, Integer pageNum, Integer pageSize) {
        // 1.调用rpc查询分页
        PageBeanVo pageBeanVo = commentService.findByPage(new ObjectId(publishId), 2, pageNum, pageSize);
        // 2.封装vo
        // 2.1 声明voList
        List<CommentVo> voList = new ArrayList<>();
        // 2.2 获取commentList
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        // 2.3 遍历commentList
        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                // 创建vo
                CommentVo vo = new CommentVo();
                vo.setId(comment.getId().toHexString());// 评论id
                UserInfo userInfo = userInfoService.findUserInfoById(comment.getUserId());
                vo.setAvatar(userInfo.getAvatar()); // 评论人头像
                vo.setNickname(userInfo.getNickname()); // 评论人
                vo.setContent(comment.getContent()); // 评论内容
                vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated()))); // 评论时间

                // 添加到集合
                voList.add(vo);
            }
        }
        // 3.将vo设置到分页对象
        pageBeanVo.setItems(voList);
        // 4.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

    //分页查询视频列表
    public ResponseEntity findVideoByPage(Long userId, Integer pageNum, Integer pageSize) {
        //调用rpc查询
        PageBeanVo pageBeanVo = videoService.findMyVideoByPage(userId, pageNum, pageSize);
        //封装vo
        List<VideoVo> videoVos = new ArrayList<>();
        //获取videos
        List<Video> items = (List<Video>) pageBeanVo.getItems();
        //遍历
        if (CollectionUtil.isNotEmpty(items)) {
            for (Video item : items) {
                VideoVo videoVo = new VideoVo();
                //封装userInfo
                videoVo.setUserInfo(userInfoService.findUserInfoById(item.getUserId()));
                //封装video
                videoVo.setVideo(item);
                //添加videoVos
                videoVos.add(videoVo);
            }
        }
        pageBeanVo.setItems(videoVos);
        return ResponseEntity.ok(pageBeanVo);
    }
    @DubboReference
    private FreezeService freezeService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //冻结用户
    public void freezeByUser(Long userId, int freezingTime, int freezingRange, String reasonsForFreezing, String frozenRemarks) {
        //冻结用户
        Freeze freeze = new Freeze();
        freeze.setUserId(userId);
        freeze.setFreezingTime(freezingTime);
        freeze.setFreezingRange(freezingRange);
        freeze.setReasonsForFreezing(reasonsForFreezing);
        if (frozenRemarks.equals(""))frozenRemarks="无";
        freeze.setFrozenRemarks(frozenRemarks);
        //设置开始时间
        freeze.setStartTime(new Date());
        //设置预计解封时间
        Integer time = 0;
        if (freezingTime == 1) time = 3;
        if (freezingTime == 2) time = 7;
        if (freezingTime == 3) time = 36500;//约100年
        freeze.setFinishTime(DateUtil.offsetDay(new Date(), time));
        freezeService.freezeByUser(freeze);

        //存储到redis中
        stringRedisTemplate.opsForValue().set("freeze:" + userId, "" + freezingRange, Duration.ofDays(time));
    }




    //解冻流程
    public void unfreezeByUser(Long userId, String reasonsForThawing) {
        //解冻用户
        freezeService.unFreezeByUser(userId,reasonsForThawing);
        //清除redis
        stringRedisTemplate.delete("freeze:" + userId);


    }
}
